package com.zzq.demo.tio.common.bs;

import com.zzq.demo.tio.common.Const;
import com.zzq.demo.tio.common.HelloPacket;
import com.zzq.demo.tio.common.packet.BaseBody;
import org.tio.core.ChannelContext;
import org.tio.utils.json.Json;

/**
 * @author : zhaoziqian
 * @description :
 * @date : 2019/10/24 10:31
 */
public abstract class AbsBsHandler<T extends BaseBody> implements BsHandler {

    public abstract Class<T> bodyClass();

    @Override
    public Object handler(HelloPacket packet, ChannelContext channelContext) throws Exception {
        String jsonStr = null;
        T bsBody = null;
        if (packet.getBody() != null) {
            jsonStr = new String(packet.getBody(), Const.CHARSET);
            bsBody = Json.toBean(jsonStr, bodyClass());
        }

        return handler(packet, bsBody, channelContext);
    }

    /**
     * @param packet
     * @param bsBody
     * @param channelContext
     * @return
     * @throws Exception
     */
    public abstract Object handler(HelloPacket packet, T bsBody, ChannelContext channelContext) throws Exception;
}
